package com.winit.cloudlink.rabbitmq.mgmt;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.Collection;

import com.google.common.base.Optional;
import com.winit.cloudlink.rabbitmq.mgmt.model.federation.ExchangeFederationOptions;
import com.winit.cloudlink.rabbitmq.mgmt.model.shovel.ShovelOptions;
import com.winit.cloudlink.rabbitmq.mgmt.model.shovel.ShovelOptionsBuilder;
import com.winit.cloudlink.rabbitmq.test.ExchangeMatcher;
import com.winit.cloudlink.rabbitmq.test.RabbitAssert;
import com.winit.cloudlink.rabbitmq.test.ShovelLinkMatcher;
import com.winit.cloudlink.rabbitmq.test.ShovelLinkMatchers;
import org.junit.Test;

import com.winit.cloudlink.rabbitmq.mgmt.model.shovel.ShovelLink;
import com.winit.cloudlink.rabbitmq.mgmt.model.shovel.ShovelStatus;

public class ShovelIT {

    @Test
    public void testEstablishConnection() {
        String shovelName = "test";
        RabbitMgmtService mgmt = RabbitMgmtService.builder()
                .host("172.16.2.176")
                .port(15672)
                .credentials("mgmt", "admin123")
                .build();

        ShovelOptions shovelOperations = new ShovelOptionsBuilder(new ShovelOptions())
                .ackOnPublish()
                .prefetchCount(1)
                .reConnectDelay(1)
                .srcUri("amqp://admin:admin123@127.0.0.1/au")
                .srcQueue("WINIT.SHOVEL.TO.CNR")
                .destUri("amqp://admin:admin123@127.0.0.1/cn")
                .destExchange("winit_receive").build();

        mgmt.shovel().establishConnection(shovelName, shovelOperations);
        ShovelLink shovelLink = mgmt.shovel().link(shovelName).get();
        assertNotNull(shovelLink);
        assertTrue(shovelName.equals(shovelLink.getName()));
        mgmt.shovel().deleteConnection(shovelName);
        ShovelLink shovelLink2 = mgmt.shovel().link(shovelName).orNull();
        assertNull(shovelLink2);
        RabbitAssert rabbitAssert = new RabbitAssert(mgmt);

        rabbitAssert.hasShovelLink("/", ShovelLinkMatchers.name("%AUR%"));
    }

    @Test
    public void testLinks() {
        RabbitMgmtService mgmt = RabbitMgmtService.builder()
                .host("172.16.2.176")
                .port(15672)
                .credentials("guest", "guest")
                .build();

        Collection<ShovelLink> val = mgmt.shovel().links("/");

        System.out.println(">>>>>>>>>>>>>>>" + val.size());
        assertTrue(val.size() > 0);

        Collection<ShovelStatus> staus = mgmt.shovel().status();
        assertTrue(staus.size() > 0);
    }
}
